package android.slc.popup.fast;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.appcompat.widget.ListPopupWindow;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PopupWindowBuilder {
    private final Context mContext;
    private final ListPopupWindow mListPopupWindow;

    public PopupWindowBuilder(Context context) {
        this.mContext = context;
        mListPopupWindow = new ListPopupWindow(context);
    }

    public PopupWindowBuilder setAdapter(@Nullable ListAdapter adapter) {
        mListPopupWindow.setAdapter(adapter);
        return this;
    }

    public PopupWindowBuilder setPromptPosition(int position) {
        mListPopupWindow.setPromptPosition(position);
        return this;
    }

    public PopupWindowBuilder setSoftInputMode(int mode) {
        mListPopupWindow.setSoftInputMode(mode);
        return this;
    }

    public PopupWindowBuilder setListSelector(Drawable selector) {
        mListPopupWindow.setListSelector(selector);
        return this;
    }

    public PopupWindowBuilder setBackgroundDrawable(@Nullable Drawable d) {
        mListPopupWindow.setBackgroundDrawable(d);
        return this;
    }

    public PopupWindowBuilder setAnimationStyle(@StyleRes int animationStyle) {
        mListPopupWindow.setAnimationStyle(animationStyle);
        return this;
    }

    public PopupWindowBuilder setAnchorView(@Nullable View anchor) {
        mListPopupWindow.setAnchorView(anchor);
        return this;
    }

    public PopupWindowBuilder setHorizontalOffset(int offset) {
        mListPopupWindow.setHorizontalOffset(offset);
        return this;
    }

    public PopupWindowBuilder setVerticalOffset(int offset) {
        mListPopupWindow.setVerticalOffset(offset);
        return this;
    }

    public PopupWindowBuilder setDropDownGravity(int gravity) {
        mListPopupWindow.setDropDownGravity(gravity);
        return this;
    }

    public PopupWindowBuilder setWidth(int width) {
        mListPopupWindow.setWidth(width);
        return this;
    }

    public PopupWindowBuilder setContentWidth(int width) {
        mListPopupWindow.setContentWidth(width);
        return this;
    }

    public PopupWindowBuilder setHeight(int height) {
        mListPopupWindow.setHeight(height);
        return this;
    }

    public PopupWindowBuilder setWindowLayoutType(int layoutType) {
        mListPopupWindow.setWindowLayoutType(layoutType);
        return this;
    }

    public PopupWindowBuilder setOnItemClickListener(@Nullable AdapterView.OnItemClickListener clickListener) {
        mListPopupWindow.setOnItemClickListener(clickListener);
        return this;
    }

    public PopupWindowBuilder setOnItemSelectedListener(@Nullable AdapterView.OnItemSelectedListener selectedListener) {
        mListPopupWindow.setOnItemSelectedListener(selectedListener);
        return this;
    }

    public PopupWindowBuilder setView(@Nullable View view) {
        mListPopupWindow.setPromptView(view);
        return this;
    }

    public Context getContext() {
        return mContext;
    }

    public ListPopupWindow create() {
        return mListPopupWindow;
    }

    /**
     * 基础的适配器
     *
     * @param <T>
     * @param <VH>
     */
    public static abstract class BaseListAdapter<T, VH extends BaseViewHolder> extends BaseAdapter {

        protected Context mContext;

        protected List<T> mList;
        protected ListView mListView;
        protected LayoutInflater inflater;

        public BaseListAdapter(Context context) {
            this.mContext = context;
            inflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() {
            return mList == null ? 0 : mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList == null ? null : mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressWarnings("unchecked")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            VH holder;
            if (convertView == null || convertView.getTag() == null) {
                convertView = inflater.inflate(getLayoutResource(), parent, false);
                holder = onCreateViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (VH) convertView.getTag();
            }

            onBindViewHolder(position, holder);
            return convertView;
        }

        public abstract int getLayoutResource();

        public abstract VH onCreateViewHolder(View convertView);

        public abstract void onBindViewHolder(int position, VH holder);


        public void setList(List<T> list) {
            this.mList = list;
        }

        public List<T> getList() {
            return mList;
        }

        public void setList(T[] list) {
            ArrayList<T> arrayList = new ArrayList<>(list.length);
            arrayList.addAll(Arrays.asList(list));
            setList(arrayList);
        }

        public ListView getListView() {
            return mListView;
        }

        public void setListView(ListView listView) {
            mListView = listView;
        }

        public void add(T t) {
            this.mList.add(t);
            notifyDataSetChanged();
        }

        public void addAll(List<T> list) {
            this.mList.addAll(list);
            notifyDataSetChanged();
        }

        public void remove(int position) {
            this.mList.remove(position);
            notifyDataSetChanged();
        }

        public void clear() {
            this.mList.clear();
            notifyDataSetChanged();
        }

    }

    /**
     * 用于ListView 的ViewHolder
     */
    public static class BaseViewHolder {
        public final View itemView;

        public BaseViewHolder(View itemView) {
            if (itemView == null) {
                throw new IllegalArgumentException("itemView may not be null");
            } else {
                this.itemView = itemView;
            }
        }

    }
}